﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using CommonSense.UI.MVVM.ViewModels;
using CommonSense.UI.Shell.Services;
using CommonSense.UI.Shell.Views;
using CommonSense.UI.Shell.Interfaces;

namespace CommonSense.UI.Shell.ViewModels
{
    class RootViewModel : BaseViewModel
    {
        private int selectedIndex;
        private BaseViewModel selectedItem;

        public event EventHandler SelectedItemChanged;
        public event EventHandler SelectedItemDirtyChanged;

        public int SelectedIndex
        {
            get { return this.selectedIndex; }
            set
            {
                if (this.selectedIndex != value)
                {
                    this.selectedIndex = value;
                    OnPropertyChanged();
                }
            }
        }

        public BaseViewModel SelectedItem
        {
            get { return this.selectedItem; }
            set
            {
                if (this.selectedItem != value)
                {
                    this.selectedItem = value;
                    this.OnPropertyChanged();
                    if (this.SelectedItemChanged != null)
                    {
                        this.SelectedItemChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        public bool TabVisiblity
        {
            get { return this.OpenedItems.Count > 0; }
        }

        public ObservableCollection<BaseViewModel> OpenedItems
        {
            get;
            private set;
        }
                
        public RootViewModel()
        {
            this.OpenedItems = new ObservableCollection<BaseViewModel>();
        }

        public void OpenItem(BaseViewModel viewModel)
        {
            viewModel.Closed += this.ViewModelClosed;
            ISaveable saveable = viewModel as ISaveable;
            if (saveable != null)
            {
                saveable.IsDirtyChanged += this.ViewModelDirtyChanged;
            }

            this.OpenedItems.Add(viewModel);
            this.SelectedIndex = this.OpenedItems.Count - 1;
            this.OnPropertyChanged(() => this.TabVisiblity);
            viewModel.OnOpened();
        }

        public void CloseItem(BaseViewModel viewModel)
        {
            viewModel.Closed -= this.ViewModelClosed;
            ISaveable saveable = viewModel as ISaveable;
            if (saveable != null)
            {
                saveable.IsDirtyChanged -= this.ViewModelDirtyChanged;
            }
            
            this.OpenedItems.Remove(viewModel);
            this.SelectedIndex = this.OpenedItems.Count - 1;
            this.OnPropertyChanged(() => this.TabVisiblity);
        }

        private void ViewModelClosed(object sender, EventArgs e)
        {
            BaseViewModel viewModel = (BaseViewModel)sender;
            this.CloseItem(viewModel);
        }

        private void ViewModelDirtyChanged(object sender, EventArgs e)
        {
            if (this.SelectedItemDirtyChanged != null)
            {
                this.SelectedItemDirtyChanged(this, e);
            }
        }
    }
}
